﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
/*
 * Driver properties
 *
 * string Name
 * string Surname
 * int Id
 * string PhoneNumber
 * string Email
 * 
 */
using System.Xml;

namespace DataAccessLayer
{

    /// <summary>
    /// The driver.
    /// Operations with data base.
    /// </summary>
    public class Driver : DalEntity
    {
        /// <summary>
        /// The DB - DAL properties mapping.
        /// Item1 - name,
        /// Item2 - dbName,
        /// Item3 - sqlParam,
        /// Item4 - type,
        /// Item5 - size
        /// </summary>
        private static List<Tuple<string, string, string, SqlDbType, int>> properties;

        /// <summary>
        /// The DB - DAL properties mapping.
        /// Item1 - name,
        /// Item2 - dbName,
        /// Item3 - sqlParam,
        /// Item4 - type,
        /// Item5 - size
        /// </summary>
        private static List<Tuple<string, string, string, SqlDbType, int>> carDriverProperties; 

        static Driver()
        {
            properties = new List<Tuple<string, string, string, SqlDbType, int>>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("C:\\Users\\Robert Sargsyan\\Documents\\Visual Studio 2012\\Projects\\Tracking\\DriverArgs.xml");
            foreach (XmlNode arg in xmlDoc.DocumentElement)
            {
                properties.Add(new Tuple<string, string, string, SqlDbType, int>(arg.Attributes["name"].Value, arg.Attributes["dbName"].Value,
                    arg.Attributes["sqlParam"].Value,
                    (SqlDbType)Enum.Parse(typeof(SqlDbType), arg.Attributes["type"].Value),
                    Convert.ToInt32(arg.Attributes["size"].Value)));
            }

            carDriverProperties = new List<Tuple<string, string, string, SqlDbType, int>>();
            xmlDoc = new XmlDocument();
            xmlDoc.Load("C:\\Users\\Robert Sargsyan\\Documents\\Visual Studio 2012\\Projects\\Tracking\\CarDriverArgs.xml");
            foreach (XmlNode arg in xmlDoc.DocumentElement)
            {
                carDriverProperties.Add(new Tuple<string, string, string, SqlDbType, int>(arg.Attributes["name"].Value, arg.Attributes["dbName"].Value,
                    arg.Attributes["sqlParam"].Value,
                    (SqlDbType)Enum.Parse(typeof(SqlDbType), arg.Attributes["type"].Value),
                    Convert.ToInt32(arg.Attributes["size"].Value)));
            }
        }

        /// <summary>
        /// Returns a reader on all drivers.
        /// If Id specified in arguments, returns a reader on drivers, who drove the car with Id.
        /// </summary>
        public override Reader ReadAll(Dictionary<string, object> args)
        {
            if (!args.ContainsKey("Id") || (int)args["Id"] < 0) //Read all drivers
            {
                SqlCommand command = new SqlCommand("ReadAllDrivers");

                Reader reader = new Reader(command, properties);

                return reader;
            }
            else  //Read drivers, who drove the car with Id
            {
                SqlCommand command = new SqlCommand("ReadDriversWithCarId");
                SqlParameter carId = command.Parameters.Add("@carId", SqlDbType.Int);
                carId.Direction = ParameterDirection.Input;
                carId.Value = args["Id"];

                Reader reader = new Reader(command, carDriverProperties);

                return reader;
            }
        }

        /// <summary>
        /// Read the driver by id.
        /// </summary>
        public override Dictionary<string, object> ReadById(Dictionary<string, object> args)
        {
            return _readById("ReadDriverById", properties, args, "driver");
        }

        /// <summary>
        /// Creates a driver in DB.
        /// </summary>
        public override Dictionary<string, object> Create()
        {
            return _create("CreateDriver");
        }



        /// <summary>
        /// Writes the driver in DB
        /// if argumens are CarId, DriverId, From, To, writes CarDriver
        /// </summary>
        public override bool? Write(Dictionary<string, object> args)
        {
            if (args.ContainsKey("CarId"))
            {
                _write("WriteCarDriver", args, carDriverProperties);
                return true;
            }

            _write("WriteDriver", args, properties);
            return true;
        }




        /// <summary>
        /// Removes the driver from DB.
        /// if arguments are CarId, Time, removes CarDrivers before that time
        /// </summary>
        public override bool? Remove(Dictionary<string, object> args)
        {
            if (args.ContainsKey("CarId"))
            {
                ValidateArguments(args, "CarId");
                ValidateArguments(args, "Time");
                SqlCommand command = new SqlCommand("RemoveCarDrivers");
                SqlParameter carId = command.Parameters.Add("@carId", SqlDbType.Int);
                carId.Direction = ParameterDirection.Input;
                carId.Value = args["CarId"];
                SqlParameter time = command.Parameters.Add("@time", SqlDbType.DateTime);
                time.Direction = ParameterDirection.Input;
                time.Value = args["Time"];

                using (SqlConnection connection = Reader.CreateConnection(command))
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }

                return true;
            }

            _remove("RemoveDriver", args);
            return true;
        }
    }
}
